﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;
using Shohdi.DataHandler;

public partial class MyFamilyAR : System.Web.UI.Page
{
    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }

   


    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!Page.IsPostBack)
        {

            

            checkSecurityAndCustomizePage();


            //bindGrid();

        }
    }


    ///// <summary>
    ///// this function will bind the grid with his family
    ///// </summary>
    //private void bindGrid()
    //{
    //    List<Persons> lstNew = mobjPersons.lstFamily;

    //    grdCourses.DataSource = lstNew;
    //    grdCourses.DataBind();
    //}

   


    /// <summary>
    /// this function will check the user previliges and  customize page
    /// </summary>
    protected void checkSecurityAndCustomizePage()
    {
        Persons currentPerson = GlobalVars.getCurrentPersonAR();
        mobjPersons = currentPerson;


        if (mobjPersons.PersonTypeID == 3)
            Response.Redirect("HomeAR.aspx");

        //if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Registered_courses_page))
        //{
        //    Response.Redirect("loginEN.aspx");
        //    return;
        //}


        
        

    }





    //protected void grdCourses_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //{

    //        e.Cancel = true;

    //    if (Page.IsValid)
    //    {

    //        TextBox txtMail = (TextBox)grdCourses.Rows[e.RowIndex].FindControl("txtMail");


    //        long perID = long.Parse(grdCourses.DataKeys[e.RowIndex]["GenID"].ToString());


    //        PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
    //        List<Persons> lstCurrent = objPersonsDB.getPersonsByGenID(perID);
    //        if (lstCurrent.Count > 0)
    //        {
    //            lstCurrent[0].Email = txtMail.Text;
    //            objPersonsDB.Update(lstCurrent[0]);
    //        }


    //    }

    //    grdCourses.Rows[e.RowIndex].RowState = DataControlRowState.Normal;

    //    checkSecurityAndCustomizePage();


    //    //bindGrid();


    //}
    protected void grdCourses_RowEditing(object sender, GridViewEditEventArgs e)
    {
        

       
       
    }

    ClassDataHandler con = new ClassDataHandler(ConnectionStrings.LMS);

    protected void odsPersons_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        checkSecurityAndCustomizePage();
        
        con.OpenConnection();
        e.InputParameters["con"] = con;
        e.InputParameters["strWhere"] = " where Badge='" + mobjPersons.Badge + "' AND PersonTypeID = '3' ";

    }
    

    protected void Page_PreRender(object sender, EventArgs e)
    {
       


        
    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        con.CloseConnection();
    }





    protected void grdCourses_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        e.Cancel = true;

        if (Page.IsValid)
        {

            TextBox txtMail = (TextBox)grdCourses.Rows[e.RowIndex].FindControl("txtMail");


            long perID = long.Parse(grdCourses.DataKeys[e.RowIndex]["GenID"].ToString());


            PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
            List<Persons> lstCurrent = objPersonsDB.getPersonsByGenID(perID);
            if (lstCurrent.Count > 0)
            {
                lstCurrent[0].Email = txtMail.Text;
                objPersonsDB.Update(lstCurrent[0]);

                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert ('تم الحفظ بنجاح');", true);

            }


        }


        grdCourses.Rows[e.RowIndex].RowState = DataControlRowState.Normal;       


        
        
        grdCourses.DataBind();

        
    }
}